package channel

import (
	"context"
	schema "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/tenant/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type TenantChannelUpiModel struct {
	ctx context.Context
	db  *gorm.DB
}

func NewTenantChannelUpiModel(ctx context.Context, db *gorm.DB) *TenantChannelUpiModel {
	return &TenantChannelUpiModel{
		ctx: ctx,
		db:  db,
	}
}

func (model *TenantChannelUpiModel) getDb() *gorm.DB {
	if model.db != nil {
		return model.db
	}

	return svc.Db
}

// CreateUpi 创建新的UPI记录
func (model *TenantChannelUpiModel) CreateUpi(upi *schema.TenantChannelUpi) error {
	res := model.getDb().Create(upi)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "tenantChannel-CreateUpi,err", err)
		return err
	}
	return nil
}

// GetUpiById 根据ID查询UPI记录
func (model *TenantChannelUpiModel) GetUpiById(id int64) (*schema.TenantChannelUpi, error) {
	var upi schema.TenantChannelUpi
	res := model.getDb().First(&upi, id)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "tenantChannel-GetUpiById,err", err)
		return nil, err
	}
	return &upi, nil
}

// GetAllUpi 根据卡号、银行名称和持卡人查询UPI列表
func (model *TenantChannelUpiModel) GetAllUpi(upi string) ([]schema.TenantChannelUpi, error) {
	var upis []schema.TenantChannelUpi
	query := model.getDb().Model(&schema.TenantChannelUpi{})

	if upi != "" {
		query = query.Where("upi", upi)
	}

	res := query.Find(&upis)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "tenantChannel-GetAllUpi,err", err)
		return nil, err
	}
	return upis, nil
}

// UpdateUpi 更新指定的UPI记录
func (model *TenantChannelUpiModel) UpdateUpi(id int64, updates map[string]interface{}) error {
	res := model.getDb().Model(&schema.TenantChannelUpi{}).Where("id = ?", id).Updates(updates)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "tenantChannel-UpdateUpi,err", err)
		return err
	}
	return nil
}

// DeleteUpi 删除指定的UPI记录
func (model *TenantChannelUpiModel) DeleteUpi(id int64) error {
	res := model.getDb().Where("id = ?", id).Delete(&schema.TenantChannelUpi{})
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "tenantChannel-DeleteUpi,err", err)
		return err
	}
	return nil
}

func (model *TenantChannelUpiModel) GetUpiIsLimit() (int32, error) {
	var upi schema.TenantChannelUpi
	res := model.getDb().Model(&schema.TenantChannelUpi{}).First(&upi)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-GetUpiIsLimit,err", err)
		return 0, err
	}
	return upi.IsLimit, nil
}

func (model *TenantChannelUpiModel) UpdateUpiIsLimit(isLimit int32) error {
	var total int64
	model.getDb().Model(&schema.TenantChannelUpi{}).Count(&total)
	if total == 0 {
		return nil
	}
	updates := schema.TenantChannelUpi{
		IsLimit: isLimit,
	}
	res := model.getDb().Model(&schema.TenantChannelUpi{}).Where("id>0").Updates(updates)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "tenantChannel-UpdateUpiIsLimit,err", err)
		return err
	}
	return nil
}
